package elm

import (
	"awesomeProject2/product"
	"awesomeProject2/write_line"
	"io/ioutil"
	"log"
	"strconv"
)

func WriteFileElm() {
	files, err := ioutil.ReadDir("C:\\Users\\admin\\GolandProjects\\waimaigo\\json\\product_elm\\")
	if err != nil {
		log.Fatal(err)
	}

	//

	cp := make([]string, 0)
	price := make([]string, 0)
	activity := make([]string, 0)

	for _, file := range files {
		producrts := product.GetProduct("C:\\Users\\admin\\GolandProjects\\waimaigo\\json\\product_elm\\" + file.Name())

		for _, datum := range producrts.Data.Data {
			//println(datum.Title + "=" + strconv.Itoa(int(datum.Price)) + "=" + strconv.Itoa(int(datum.MinActivePrice)))
			cp = append(cp, datum.Title)
			price = append(price, strconv.FormatFloat(datum.Price, 'f', 2, 64))
			if datum.MinActivePrice == 0 {
				activity = append(activity, strconv.FormatFloat(datum.Price/2, 'f', 2, 64))
			} else {
				activity = append(activity, strconv.FormatFloat(datum.MinActivePrice, 'f', 2, 64))
			}
		}
	}

	write_line.WriteLineTxm(cp, "C:\\Users\\Admin\\go\\src\\awesomeProject2\\elm\\产品.txt")
	write_line.WriteLineTxm(price, "C:\\Users\\Admin\\go\\src\\awesomeProject2\\elm\\price.txt")
	write_line.WriteLineTxm(activity, "C:\\Users\\Admin\\go\\src\\awesomeProject2\\elm\\activity.txt")
}

func GetFileElm() []product.Products {
	files, err := ioutil.ReadDir("C:\\Users\\admin\\GolandProjects\\waimaigo\\json\\product_elm\\")
	if err != nil {
		log.Fatal(err)
	}

	mts := make([]product.Products, 0)

	for _, file := range files {
		producrts := product.GetProduct("C:\\Users\\admin\\GolandProjects\\waimaigo\\json\\product_elm\\" + file.Name())
		mts = append(mts, producrts)
	}
	return mts
}
